<template>
  <div class="login">
    <div class="loginbox">
      <!-- 图标 -->
      <div class="logo">
        <img src="@/assets/common/login-logo.png" alt="">
      </div>
      <!-- 登录表单 -->
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" prefix-icon="el-icon-user-solid" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter.native="submit"
          />
          <!-- 由于el-input不是原生标签,不能直接调用submit方法,要调用需要this.$emit(方法名) -->
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
      <!-- 注册链接 -->
      <div class="register">
        <a href="#">还没有账号?立即注册</a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      // 设置验证规则
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确的手机号', trigger: 'blur' },
          {
            // 自定义验证规则
            // rules规则(一般不用),value表单的值,callback回调函数
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback() // 验证成功的回调函数
              } else {
                callback(new Error('请输入正确手机号')) // 验证失败的回调函数
              }
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '请输入6~12位密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击登录按钮,验证表单规则
    submit() {
      // 使用refs获取表单元素,调用validate方法
      this.$refs.form.validate(async(result) => {
        // result为boolean值
        if (result) {
          this.loading = true
          try {
            await this.$store.dispatch('user/toLogin', this.form)
            this.loading = false
            console.log('ddd')
            this.$router.push('/')
          } catch (error) {
            this.loading = false
            console.log(error)
          }
        } else {
          console.log('验证失败!')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/*
  @代表src只能用于js与html,不能用于css,css用~@
  scoped: 只管当前组件与子组件最外层
  v-html的东西,也管不到
  插槽内的内容可以管到
  希望它管到: >>>  /deep/  ::v-deep 希望用到的class(...)
*/
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/common/login.jpg);
  display: flex;
  justify-content: center;
  align-items: center;
  .loginbox {
    width: 580px;
    height: 500px;
    // background-color: #fff;
    .logo {
      width: 100%;
      // height: 100%;
      margin-bottom: 20px;
      img {
        width: 100%;
      }
    }
    ::v-deep .el-input {
      margin-bottom: 10px;
      input.el-input__inner {
        padding: 10px;
        padding-left: 50px;
        height: 60px;
        line-height: 60px;
        background-color: #d3e4ff;
        color: #77b0fe;
      }
      i.el-input__icon {
        height: 60px;
        line-height: 60px;
        padding-left: 10px;
        font-size: 18px;
        color: #889aa4;
      }
    }
    .el-button {
      width: 100%;
      border: 0;
      height: 60px;
      // line-height: 50px;
      font-size: 20px;
      background-color: #407ffe;
      color: #fff;
    }
    .register {
      color: #fff;
      font-size: 18px;
      text-align: center;
    }
  }
}
</style>
